#!/usr/bin/env python

import sqlite3  
import sys
import urllib2

if len(sys.argv) < 2:
	print "Usage: ./tag_cloud.py user (artist_tags|track_tags)"
	sys.exit(1)

user = sys.argv[1]

type = "artist_tags"
if len(sys.argv) > 2:
	type = sys.argv[2]

font_min = 8
font_max = 100

conn = sqlite3.connect("lastfm-%s.db" % user)
c = conn.cursor()

tags = [(artist,playcount) for artist,playcount in c.execute("SELECT %s AS tag,SUM(playcount) AS plays FROM %s GROUP BY tag HAVING plays > 20" % (type[:-1],type))]

tags.sort()

max_plays = max(tags,key=lambda (a,p): p)[1]

print "<html>"
for artist,playcount in tags:
  print "<span style='font-size: %dpx;'><a href='http://www.last.fm/tag/%s/artists'>%s</a></span>, " % ((font_max-font_min)*playcount/max_plays+font_min,urllib2.quote(urllib2.quote(artist.encode("utf8"))),artist)
print "</html>"
